package Presentation;

import Metier.Controleurs.GestionCuisson;
import Metier.Objets.LotCuisson;
import Metier.Utils.DateUtils;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.UIManager;

public class TuileCuisson extends javax.swing.JPanel {

    private JButton btnMiseVitrine;
    private JButton btnSupprimer;

    private final JLabel lblQuantite;
    private final JLabel lblQuantiteVal;
    private final JLabel lblMinuterie;
    private final JLabel lblMinuterieVal;

    private final Timer timer;

    private LotCuisson lotCuisson;

    private final Onglet onglet;

    private ResourceBundle cuissonBundle;

    public TuileCuisson(LotCuisson lotCuisson, Onglet onglet) {
        this.cuissonBundle = ResourceBundle.getBundle("ressources/Cuisson/Cuisson", onglet.getFP().getLocale());
        GestionCuisson gestionCuisson = new GestionCuisson(onglet);
        this.lotCuisson = lotCuisson;
        this.onglet = onglet;
        GridBagLayout gbl = new GridBagLayout();
        gbl.columnWeights = new double[]{Double.MIN_VALUE, Double.MIN_VALUE, Double.MIN_VALUE};
        this.setLayout(gbl);
        this.setBorder(BorderFactory.createTitledBorder(this.lotCuisson.getNom()));
        this.btnSupprimer = new JButton("X");
        this.btnSupprimer.setBackground(Color.decode("#e53935"));
        this.btnSupprimer.setForeground(Color.white);
        this.btnSupprimer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int choice = JOptionPane.showConfirmDialog(onglet,
                        cuissonBundle.getString("cuisson.confirmationjeter"),
                        cuissonBundle.getString("cuisson.confirmation"),
                        JOptionPane.YES_NO_OPTION);
                if (choice == JOptionPane.YES_OPTION) {
                    btnSupprimer.setEnabled(false);
                    gestionCuisson.supprimerCuisson(lotCuisson);
                    onglet.rafraichir();
                }
            }
        });

        this.btnMiseVitrine = new JButton(cuissonBundle.getString("cuisson.misevitrine"));
        this.btnMiseVitrine.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                btnMiseVitrine.setBackground(UIManager.getColor("JButton.background"));
                btnMiseVitrine.setEnabled(false);
                gestionCuisson.mettreEnVitrine(lotCuisson);
                onglet.rafraichir();
            }
        });
        this.btnMiseVitrine.setEnabled(false);

        this.lblQuantite = new JLabel(cuissonBundle.getString("cuisson.quantite") + " : ");
        this.lblQuantiteVal = new JLabel(Integer.toString(this.lotCuisson.getQuantite()));

        this.lblMinuterie = new JLabel(cuissonBundle.getString("cuisson.tempsrestant") + " : ");
        this.lblMinuterieVal = new JLabel();
        
        this.timer = new Timer(900, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateTime();
            }
        });
        timer.start();
        this.updateTime();

        GridBagConstraints gbc = new GridBagConstraints(2, 0,
                1, 1,
                0, 0,
                GridBagConstraints.CENTER,
                GridBagConstraints.NONE,
                new Insets(5, 0, 5, 0),
                0, 0
        );
        this.add(this.btnSupprimer, gbc);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.LINE_END;
        this.add(this.lblQuantite, gbc);
        gbc.gridy = 1;
        this.add(this.lblMinuterie, gbc);
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.LINE_START;
        this.add(this.lblQuantiteVal, gbc);
        gbc.gridy = 1;
        this.add(this.lblMinuterieVal, gbc);
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 3;
        gbc.anchor = GridBagConstraints.CENTER;
        this.add(this.btnMiseVitrine, gbc);
    }

    public void updateTime() {
        long nbSeconds = timeLeft();
        if (nbSeconds < 0) {
            this.timer.stop();
            this.lblMinuterieVal.setText(cuissonBundle.getString("cuisson.termine"));
            this.btnMiseVitrine.setForeground(Color.white);
            this.btnMiseVitrine.setBackground(Color.decode("#4caf50"));
            this.btnMiseVitrine.setEnabled(true);
        } else {
            this.lblMinuterieVal.setText(DateUtils.secondsToMinuteAndSeconds(nbSeconds));
        }
    }

    public long timeLeft() {
       return DateUtils.secondsLeft(this.lotCuisson.getDate(),
                this.lotCuisson.getProduit().getTempsCuisson());
    }

    public LotCuisson getLotCuisson() {
        return lotCuisson;
    }

    public void setLotCuisson(LotCuisson lotCuisson) {
        this.lotCuisson = lotCuisson;
    }
}
